<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2018/9/11
 * Time: 17:21
 */

header("content-type:text/html;charset=utf-8");         //设置编码
require_once 'Medoo.php';
require_once 'Contact.php';

/**
 * 获取所有实时状态 GET
 *
 * 返回数据：
 *
 * -1 故障
 * 0  瑕疵
 * 1  正常
 * 2  良好
 * 3  优秀
 */


selectState();


/**
 * @return 判断是否有该条记录
 */
function hasRecoved()
{
    global $database;
    global $modularID;
    if ($database->has("manage_all_state")) {
        return true;
    } else {
        return false;
    }
}


/**
 * 查询该ID的开关状态
 */
function selectState($modularID)
{
    global $database;
    global $modularID;


    if (!hasRecoved()) {
        $result1 = array(
            'code' => urlencode(200),
            'msg' => urlencode('无数据'),
            'data' => null
        );
        echo urldecode(json_encode($result1));
    }


    $result = null;
    // 查询数据示例
    $datas = $database->select("manage_all_state", [
        "comm_state",
        "system_state",
        "hardware_heal",
        "service_state",
        "media_moudle_state",
        "visit_moudle_state",
        "monitor_moudle_state",
        "power_moudle_state",
        "comm_moudle_state",
        "hardware_state",
        "hard_monitor_state",
        "sensor_state",
        "hard_relay_state",
        "controlled_hardware_state",
        "terminal_state",
        "tran_channel_state"

    ], [
//        "modular_id[=]" => $modularID
    ]);


    foreach ($datas as $data) {
        $result0 = array(
            'comm_state' => urlencode($data["comm_state"]),
            'system_state' => urlencode($data["system_state"]),
            'hardware_heal' => urlencode($data["hardware_heal"]),
            'service_state' => urlencode($data["service_state"]),
            'media_moudle_state' => urlencode($data["media_moudle_state"]),
            'visit_moudle_state' => urlencode($data["visit_moudle_state"]),
            'monitor_moudle_state' => urlencode($data["monitor_moudle_state"]),
            'power_moudle_state' => urlencode($data["power_moudle_state"]),
            'comm_moudle_state' => urlencode($data["comm_moudle_state"]),
            'hardware_state' => urlencode($data["hardware_state"]),
            'hard_monitor_state' => urlencode($data["hard_monitor_state"]),
            'sensor_state' => urlencode($data["sensor_state"]),
            'hard_relay_state' => urlencode($data["hard_relay_state"]),
            'controlled_hardware_state' => urlencode($data["controlled_hardware_state"]),
            'terminal_state' => urlencode($data["terminal_state"]),
            'tran_channel_state' => urlencode($data["tran_channel_state"]),

        );

    }
    $result1 = array(
        'code' => urlencode(200),
        'msg' => urlencode('查询成功'),
        'data' => array_reverse($result0) //反转数组
    );

    echo urldecode(json_encode($result1));

}




